package com.jvxb.demo.controller;

import cn.hutool.core.lang.Assert;
import com.jvxb.demo.service.KafkaService;
import com.jvxb.demo.utils.ResponseDataVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

/**
 * @author jvxb
 * @since 2023-01-15
 */
@RestController
@RequestMapping("/kafka")
@Api(tags = "kafka控制器")
public class KafkaController {

    @Autowired
    KafkaService kafkaService;

    @PostMapping("/send")
    public ResponseDataVo send(@RequestBody Map<String, String> param) {
        Assert.notBlank(param.get("topic"), "topic must not be null");
        Assert.notBlank(param.get("message"), "message must not be null");
        kafkaService.send(param.get("topic"), param.get("message"));
        return ResponseDataVo.success();
    }

    @GetMapping("listTopics")
    public ResponseDataVo getTopicList() throws Exception {
        Set<String> topics = kafkaService.listTopics();
        return ResponseDataVo.success(topics);
    }

}
